package com.j.lemon.learn.asiainfo;

import com.j.lemon.learn.jdbc.DataSourceUtils;
import com.j.lemon.learn.jdbc.DbProperties;
import com.j.lemon.learn.jdbc.DbTypeEnum;
import org.apache.poi.hpsf.Decimal;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.*;
import java.util.*;

/**
 * @author lijunjun
 * @title: MetaDataTool
 * @description: 数据模型DB变更
 * @date 2023/8/2110:37
 */
public class MetaDataTool {
    public static void main(String[] args) throws SQLException {
        long propId = 1693554717295173634L;
        List<String> tables = new ArrayList<>();
        tables.add("sys_meta_entity_def");//所属分类
        tables.add("sys_biz_scene");//场景
        tables.add("sys_meta_entity_scene");//所属分类和场景关系表
        tables.add("sys_meta_prop_def");//模型字段
        tables.add("sys_meta_prop_scene");//模型字段和场景关系表
        tables.add("sys_meta_model_param");//服务传参表
        tables.add("sys_meta_prop_visible_rule");//隐藏相关表
        tables.add("sys_encrypt_strategy");//加密相关
        tables.add("sys_dm_strategy");//脱敏相关

        DbProperties dbProperties = DbProperties.builder()
                .type(DbTypeEnum.oceanbase)
                .ip("10.28.148.162")
                .port("2881")
                .database("ais_iam")
                .username("root")
                .password("Ocenbas@123")
                .driverClassName("com.alipay.oceanbase.jdbc.Driver")
                .build();
        DataSource datasource = DataSourceUtils.getDatasource(dbProperties);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);

        //sys_meta_prop_def 字段表
        List<Map<String, Object>> sysMetaPropDef = jdbcTemplate.queryForList("select * from sys_meta_prop_def where prop_id = " + propId);
        Long dmMethod = (Long) sysMetaPropDef.get(0).get("dm_method");
        generateInsertSql("sys_meta_prop_def",sysMetaPropDef);
        generateUpdateSql("sys_meta_prop_def",sysMetaPropDef,"prop_id",propId);
        //sys_meta_prop_scene  字段-场景 关系
        List<Map<String, Object>> sysMetaPropScene = jdbcTemplate.queryForList("select * from sys_meta_prop_scene where prop_id = " + propId);
        generateInsertSql("sys_meta_prop_scene",sysMetaPropScene);
        generateUpdateSql("sys_meta_prop_scene",sysMetaPropScene,"prop_id",propId);
        //sys_biz_scene 场景
        Long sceneId = (Long) sysMetaPropScene.get(0).get("scene_id");
        List<Map<String, Object>> sysBizScene = jdbcTemplate.queryForList("select * from sys_biz_scene where scene_id = " + sceneId);
        generateInsertSql("sys_biz_scene",sysBizScene);
        generateUpdateSql("sys_biz_scene",sysBizScene,"scene_id",sceneId);
        //sys_meta_entity_scene 场景和所属分类关系表
        List<Map<String, Object>> sysMetaEntityScene = jdbcTemplate.queryForList("select * from sys_meta_entity_scene where scene_id = " + sceneId);
        generateInsertSql("sys_meta_entity_scene",sysMetaEntityScene);
        generateUpdateSql("sys_meta_entity_scene",sysMetaEntityScene,"scene_id",sceneId);
        Long entityId = (Long) sysMetaEntityScene.get(0).get("entity_id");
//        StringBuilder stringBuilder = new StringBuilder();
//        for (Map<String, Object> stringObjectMap : sysMetaEntityScene) {
//            Long entityId = (Long) stringObjectMap.get("entity_id");
//            stringBuilder.append(entityId).append(",");
//        }
//        stringBuilder.delete(stringBuilder.length()-1,stringBuilder.length());
        //sys_meta_entity_def所属分类表
        List<Map<String, Object>> sysMetaEntityDef = jdbcTemplate.queryForList("select * from sys_meta_entity_def where entity_id = " + entityId);
        generateInsertSql("sys_meta_entity_def",sysMetaEntityDef);
        generateUpdateSql("sys_meta_entity_def",sysMetaEntityDef,"entity_id",entityId);
        //sys_meta_model_param 服务传参表
        List<Map<String, Object>> sysMetaModelParam = jdbcTemplate.queryForList("select * from sys_meta_model_param where prop_id = " + propId);
        generateInsertSql("sys_meta_model_param",sysMetaModelParam);
        generateUpdateSql("sys_meta_model_param",sysMetaModelParam,"prop_id",propId);
        //sys_meta_prop_visible_rule 隐藏配置相关表
        List<Map<String, Object>> sysMetaPropVisibleRule = jdbcTemplate.queryForList("select * from sys_meta_prop_visible_rule where prop_id = " + propId);
        generateInsertSql("sys_meta_prop_visible_rule",sysMetaPropVisibleRule);
        generateUpdateSql("sys_meta_prop_visible_rule",sysMetaPropVisibleRule,"prop_id",propId);
        //sys_dm_strategy 脱敏相关配置表
        List<Map<String, Object>> sysDmStrategy = jdbcTemplate.queryForList("select * from sys_dm_strategy where dm_stg_id = " + dmMethod);
        generateInsertSql("sys_dm_strategy",sysDmStrategy);
        generateUpdateSql("sys_dm_strategy",sysDmStrategy,"dm_stg_id",dmMethod);
    }

    public static void generateInsertSql(String tableName, List<Map<String, Object>> queryResult){
        for (Map<String, Object> stringObjectMap : queryResult) {
            StringBuilder insert = new StringBuilder();
            insert.append("insert into ").append(tableName).append(" (");
            Set<String> strings = stringObjectMap.keySet();
            for (String column : strings) {
                insert.append(column).append(",");
            }
            insert.delete(insert.length()-1,insert.length());
            insert.append(") values (");
            for (String column : strings) {
                Object o = stringObjectMap.get(column);
                if(o instanceof Long || o instanceof Integer || o instanceof BigInteger || o instanceof Decimal || o instanceof BigDecimal){
                    insert.append(o);
                }else if(o==null){
                    insert.append(" null ");
                }else{
                    insert.append("'").append(o).append("'");
                }
                insert.append(",");
            }
            insert.delete(insert.length()-1,insert.length());
            insert.append(");");
            System.out.println(insert);
        }
    }
    public static void generateUpdateSql(String tableName,List<Map<String, Object>> queryResult,String columnName,Long columnValue){
        for (Map<String, Object> stringObjectMap : queryResult) {
            StringBuilder update = new StringBuilder();
            update.append("update ").append(tableName).append(" set ");
            Set<String> strings = stringObjectMap.keySet();
            for (String column : strings) {
                update.append(column).append("=");
                Object o = stringObjectMap.get(column);
                if(o instanceof Long || o instanceof Integer || o instanceof BigInteger || o instanceof Decimal || o instanceof BigDecimal){
                    update.append(o);
                }else if(o==null){
                    update.append(" null ");
                }else{
                    update.append("'").append(o).append("'");
                }
                update.append(",");
            }
            update.delete(update.length()-1,update.length());
            update.append(" where ").append(columnName).append("=").append(columnValue);
            update.append(";");
            System.out.println(update);
        }
    }

}
